<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Consultant extends MY_Controller {

     function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('parser');
        //$this->template->set_template('home');
        
        $this->load->model('consultant_model', 'consultant', true);

     }

    function index($id = 0) {
        $this->view($id);
    }

    function view($id) {

        $id = intval($id);
        if($id)
            $data = $this->consultant->getById($id);
        else
             $data = $this->consultant->getNewest();
        
        if(!$data){
            $data['title'] = 'Thông tin không tồn tại';
            $data['question'] = '';
            $data['answer'] = '';
        }

        $data['site_url'] = site_url();
        $data['base_url'] = base_url();

        $offset = 0;
        $number_record = 50;
        $active = 0;
        $data['consultants'] = $this->consultant->getAllFrontEnd($offset, $number_record, $active);

        $this->template->parse_view('content', 'default/front_end/consultant/view_consultant', $data);
        $this->template->render();
    }

    function create() {
        
        $data['site_url'] = site_url();
        $data['base_url'] = base_url();
        
        $offset = 0;
        $number_record = 50;
        $active = 0;
        $data['consultants'] = $this->consultant->getAllFrontEnd($offset, $number_record, $active);

        $this->load->helper('string');
        $uid = $this->input->post('txtUID');
        if($uid){
        $data['fullname'] = $this->input->post('txtFullname');
        $data['address'] = $this->input->post('txtAddress');
        $data['email'] = $this->input->post('txtEmail');
        $data['phone'] = $this->input->post('txtPhone');
        $data['fax'] = $this->input->post('txtFax');
        $data['title'] = $this->input->post('txtTitle');
        $data['question'] = $this->input->post('txtContent');
        
        }
        else{
            $data['fullname'] = '';
            $data['address'] = '';
            $data['email'] = '';
            $data['phone'] = '';
            $data['fax'] = '';
            $data['title'] = '';
            $data['question'] = '';
        }
        //print_r($data);
        //$data['transport'] = $this->input->post('ddlTransport');
        $data['error_message'] = '';
        //echo "asssd";
        
        if($uid == $this->session->userdata('myuid')){

            //echo "SESSION = NHAU";
            $this->load->helper(array('form', 'url'));

            $this->load->library('form_validation');

            $this->form_validation->set_rules('txtFullname', 'Họ tên', 'required|min_length[2]|max_length[120]');
            $this->form_validation->set_rules('txtAddress', 'Địa chỉ', 'required');
            $this->form_validation->set_rules('txtEmail', 'Email', 'required|valid_email');
            $this->form_validation->set_rules('txtPhone', 'Điện thoại', 'required|min_length[10]|max_length[11]');
            $this->form_validation->set_rules('txtFax', 'Fax', 'min_length[10]');
            $this->form_validation->set_rules('txtTitle', 'Tiêu đề', 'required|min_length[10]|max_length[80]');
            $this->form_validation->set_rules('txtContent', 'Nội dung', 'required|min_length[20]|max_length[3000]');
            
            if ($this->form_validation->run() == FALSE) {
               $data['error_message'] = '<div stype="margin-left:100px; width=500px; color:red">' . validation_errors() . '</div>' ;
            }
            else{
                $this->load->model('consultant_model', 'consultant', true);
                $data['status'] = 1;
                $data['active'] = 0;
                $data['answer'] = '';
                $data['add_date'] = date('Y-m-d H:i:s', time());
                //$data['upd_date'] = date('Y-m-d H:i:s', time());
                $id = $this->consultant->create($data);
                if($id){
                    $data['message'] = "Quý khách đã gửi câu hỏi tư vấn thành công. Chúng tôi sẽ sớm trả lời câu hỏi của quý khách.
                                        Cảm ơn quý khách đã sữ dụng dịch vụ của chúng tôi.";
                    $this->session->unset_userdata('myuid');
                    
                    $data['fullname'] = '';
                    $data['address'] = '';
                    $data['email'] = '';
                    $data['phone'] = '';
                    $data['fax'] = '';
                    $data['title'] = '';
                    $data['question'] = '';
                    
                    /*
                    $this->load->library('email');

                    $config['protocol'] = 'smtp';
                    $config['smtp_host'] = 'ssl://smtp.googlemail.com';
                    $config['smtp_port'] = '465';
                    $config['smtp_user'] = 'pvkhoi@gmail.com';
                    $config['smtp_pass'] = '02329120';
                    $config['charset'] = 'utf-8';


                    $config['wordwrap'] = TRUE;

                    $this->email->initialize($config);
                    $this->email->from('pvkhoi@gmail.com', 'Phạm Minh Khởi');
                    $this->email->to('pmkhoi@gmail.com');
                    $this->email->subject('This is my subject');
                    $this->email->message('This is my message');
                    //$this->email->send();
                    if($this->email->send())
                    {
                        echo 'Your email was sent, successfully.';
                    }

                    else
                    {
                        show_error($this->email->print_debugger());khách
                    }
                    */
                }
                else{
                    $data['message'] = "Hệ thống chưa nhận được câu hỏi tư vấn của quý . Xin quý khách vui lòng thử lại một lần nữa.";
                }

                $this->template->parse_view('content', 'default/front_end/consultant/view_message_consultant', $data);
                    //$this->template->write('content', '<div class="left_col">232fgfg fg fgf</div>');
                 $this->template->render();
                 return;
                    
            }
        }

        $data['uid'] = random_string('alnum', 16);
        $this->session->set_userdata('myuid', $data['uid']);

        $this->template->parse_view('content', 'default/front_end/consultant/view_create_consultant', $data);
        $this->template->render();
    }

    function detail($id){
        $id = intval($id);
        if($id)
            $data = $this->consultant->findByID($id);
        if(!$data){
            $data['title'] = 'Thông tin không tồn tại';
            $data['question'] = '';
            $data['answer'] = '';
        }

        $data['site_url'] = site_url();
        $data['base_url'] = base_url();

        $offset = 0;
        $number_record = 50;
        $active = 0;
        $data['consultants'] = $this->consultant->getAllFrontEnd($offset, $number_record, $active);

        $this->template->parse_view('content', 'default/front_end/consultant/view_consultant', $data);
        $this->template->render();
        
    }

    function index1($var = 1212){
        echo 'call index1. var =' . $var;
    }

    function buildLeftContent() {
        $data['menuitems'] = array(
            array(
                'status' => 'active',
                'click_function' => 'get_list(current_from, this);',
                'text' => 'Add New Child',
                'href' => site_url('children/add_child'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'create_user(this);',
                'text' => 'View All Children ',
                'href' => site_url('children/view_all'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'system_logs(this);',
                'text' => 'View Current Children ',
                'href' => site_url('children/view_current'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'login_logs(this);',
                'text' => 'View Graduated Children ',
                'href' => site_url('children/view_graduated'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'system_config(this);',
                'text' => 'View Withdrawn Children ',
                'href' => site_url('children/view_withdraw'),
                'subitem' => array('a' => 'a')
            )
        );
        $this->load->library('parser');
        $this->parser->parse('default/front_end/menu_left', $data);
        $this->template->parse_view('header', 'default/front_end/menu_left', $data);
    }

}

?>